<template>
  <div class="dashboard-container">
    <div class="app-container">
      <pageTool :show-before="false">
        <template #after>
          <el-button type="primary" size="small" icon="el-icon-edit" @click="addPermission(1,'0')">添加权限</el-button>
        </template>
      </pageTool>
      <el-card>
        <el-table
          ref="table"
          :data="permissionList"
          row-key="id"
        >
          <el-table-column label="名称" prop="name">
            <template v-slot="{row}">
              <i :class="row.pid==='0'?'el-icon-folder-opened':'el-icon-view'" @click="expand(row)" />
              {{ row.name }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="标识" prop="code" />
          <el-table-column align="center" label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button v-if="row.type===1" type="text" @click="addPermission(2,row.id)">添加</el-button>
              <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
              <el-button type="text" @click="deletePermission(row.id)">删除</el-button>
            </template></el-table-column></el-table>
      </el-card>
      <!-- 更新权限的对话框 -->
      <el-dialog
        :title="(formData.id?'编辑':'添加')+'权限'"
        :visible="updateDialogVisible"
        width="50%"
        @close="onClose"
      >
        <el-form ref="form" :model="formData" :rules="rules" label-width="80px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="企业可见">
            <el-switch
              v-model="formData.enVisible"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="updateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="savePermission">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getAllPermissionAPI, addPermissionAPI, getThePermissionByIdAPI, updateThePermissionByIdAPI, deleteThePermissionByIdAPI } from '@/api/permission'
import { getTreeList } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      permissionList: [],
      updateDialogVisible: false,
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: 1,
        pid: ''
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }

    }
  },

  created() {
    this.getAllPermission()
  },

  methods: {
    async getAllPermission() {
      const res = await getAllPermissionAPI()
      this.permissionList = getTreeList('0', res)
    },
    // 展开表格子节点
    expand(row) {
      row.isExpand = !row.isExpand
      this.$refs.table.toggleRowExpansion(row, row.isExpand)
    },
    // 点击添加 显示对话框
    addPermission(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.updateDialogVisible = true
    },
    // 点击确定保存更新
    async savePermission() {
      try {
        await this.$refs.form.validate()
        if (this.formData.id) {
          // 编辑
          await updateThePermissionByIdAPI(this.formData)
          this.$message.success('编辑权限成功')
        } else {
          await addPermissionAPI(this.formData)
          this.$message.success('权限添加成功')
        }
        this.getAllPermission()
        this.updateDialogVisible = false
      } catch (error) {
        console.log(error)
      }
    },
    // 点击编辑 编辑权限
    async editPermission(id) {
      this.formData = await getThePermissionByIdAPI(id)
      this.updateDialogVisible = true
    },
    // 点击删除 删除权限
    async deletePermission(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteThePermissionByIdAPI(id)
        this.$message.success('权限删除成功')
        this.getAllPermission()
        this.updateDialogVisible = false
      }).catch(() => {
      })
    },
    // 对话框关闭
    onClose() {
      this.$refs.form.clearValidate()
      this.updateDialogVisible = false
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: 1,
        pid: ''
      }
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-table__expand-icon {
  display: none !important;
}
</style>
